package com.n5x.mapper.promotion;

import com.n5x.model.promotion.Promotion;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

import static org.apache.ibatis.jdbc.SqlBuilder.BEGIN;

public interface PromotionMapper {

    String TABLE = "PROMOTION_INFO";
    String All_FIELD = "ID,TITLE,PICTURE,CONTENT_,GOODS_ID,IS_TOP,DISABLE,CREATE_TIME";
    String FIELD_ID = "ID";
    String FIELD_TITLE = "TITLE";
    String FIELD_PICTURE = "PICTURE";
    String FIELD_CONTENT = "CONTENT_";
    String FIELD_GOODS_ID = "GOODS_ID";
    String FIELD_IS_TOP = "IS_TOP";
    String FIELD_DISABLE = "DISABLE";
    String FIELD_CREATE_TIME = "CREATE_TIME";

    @Insert("insert into " + TABLE + "(" + All_FIELD + ") values(#{id},#{title},#{picture},#{content},#{goodsId},#{isTop},#{disable},#{createTime})")
    @SelectKey(keyProperty = "id", resultType = String.class, before = true, statement = "select replace(uuid(), '-', '') as id from dual")
    int insert(Promotion Promotion);

    @UpdateProvider(type = PromotionProvider.class, method = "updatePromotion")
    int update(Promotion Promotion);

    @Select("SELECT * FROM " + TABLE + " WHERE " + FIELD_ID + " = #{id}")
    Promotion get(@Param("id") String id);

    @SelectProvider(type = PromotionProvider.class, method = "find")
    List<Promotion> find(Map<String, Object> params);


    class PromotionProvider {
        public String find(final Map<String, Object> params) {
            return new SQL() {{
                SELECT(All_FIELD);
                FROM(TABLE);
                StringBuffer sb = new StringBuffer();
                sb.append("1 = 1");
                if (StringUtils.isNotBlank((String) params.get(FIELD_ID))) {
                    sb.append(" AND " + FIELD_ID + " = #{id}");
                }
                if (StringUtils.isNotBlank((String) params.get(FIELD_TITLE))) {
                    sb.append(" AND " + FIELD_TITLE + " like #{title}");
                }
                if (params.get(FIELD_DISABLE) != null) {
                    sb.append(" AND " + FIELD_DISABLE + " = " + params.get(FIELD_DISABLE)) ;
                }
                WHERE(sb.toString());
                ORDER_BY(FIELD_CREATE_TIME);
            }}.toString();
        }

        public String updatePromotion(final Promotion promotion) {
            return new SQL() {
                {
                    BEGIN();
                    UPDATE(TABLE);
                    if (StringUtils.isNotBlank(promotion.getTitle())) {
                        SET("TITLE = #{title}");
                    }
                    if (StringUtils.isNotBlank(promotion.getPicture())) {
                        SET("PICTURE = #{picture}");
                    }
                    if (StringUtils.isNotBlank(promotion.getContent())) {
                        SET("CONTENT_ = #{content}");
                    }
                    if (StringUtils.isNotBlank(promotion.getGoodsId())) {
                        SET("GOODS_ID = #{goodsId}");
                    }
                    if (promotion.getTop() != null) {
                        SET("IS_TOP = #{isTop}");
                    }
                    if (promotion.getDisable() != null) {
                        SET("DISABLE = #{disable}");
                    }
                    WHERE(FIELD_ID + " = #{id}");
                }
            }.toString();
        }
    }
}

